var testNumber = 1;
var name = "Automatic Test ";
var nameModified;
var target = UIATarget.localTarget();
var app = target.frontMostApp();

//UIATarget.localTarget().frontMostApp().mainWindow().logElementTree();

function getWindow()
{
	return app.mainWindow();
}

function addNew()
{
	// Tap the 'Add' button
	app.navigationBar().rightButton().tap();
    
	var detailScrollView = app.mainWindow().scrollViews()[0];
	var window = getWindow();
    
	// Set Title
	name = "Automatic Test "+ testNumber;
	detailScrollView.textFields()[0].setValue(name);
    
	// Set date
	window.scrollViews()[0].buttons()[1].tap();
	window.pickers()[0].wheels()[0].tapWithOptions({tapOffset:{x:0.76, y:0.67}});
	window.pickers()[0].wheels()[1].tapWithOptions({tapOffset:{x:0.76, y:0.67}});
	window.pickers()[0].wheels()[2].tapWithOptions({tapOffset:{x:0.76, y:0.67}});
	target.delay(1);
	app.toolbar().buttons()["Done"].tap();
	
	// Set time
	window.scrollViews()[0].buttons()[2].tap();
	window.pickers()[0].wheels()[1].tapWithOptions({tapOffset:{x:0.64, y:0.68}});
	target.delay(1);
	app.toolbar().buttons()["Done"].tap();
	
	// Notes
	window.scrollViews()[0].textFields()[1].setValue("Notes");
    
	// Background Tap
	window.scrollViews()[0].buttons()[0].tap();
    
	// Set repeat
	window.scrollViews()[0].buttons()[3].tap();
	window.pickers()[0].wheels()[0].tapWithOptions({tapOffset:{x:0.41, y:0.76}});
	target.delay(1);
	app.toolbar().buttons()["Done"].tap();
    
	// Save
	app.navigationBar().rightButton().tap();
	
	if (window.tableViews()[0].cells().firstWithPredicate("name ENDSWITH [c] '"+ name +"'"))
	{
    	UIALogger.logPass("Reminder Added");
		testNumber ++;
        
		return name;
	}
	else
   		UIALogger.logFail("Reminder Not Added");
	return null;
}

function editOld(cellToEdit)
{
	var cell = app.mainWindow().tableViews()[0].cells().firstWithPredicate("name ENDSWITH [c] '"+ cellToEdit +"'");
    
	// Tap cell
	cell.tap();
    
	var window = getWindow();
    
	// Change title
	nameModified = window.scrollViews()[0].textFields()[0].value() + " Modified";
	window.scrollViews()[0].textFields()[0].setValue(nameModified);
    
	// Change date
	window.scrollViews()[0].buttons()[1].tap();
	window.pickers()[0].wheels()[0].tapWithOptions({tapOffset:{x:0.64, y:0.29}});
	window.pickers()[0].wheels()[1].tapWithOptions({tapOffset:{x:0.64, y:0.29}});
	window.pickers()[0].wheels()[2].tapWithOptions({tapOffset:{x:0.64, y:0.29}});
	target.delay(1);
	app.toolbar().buttons()["Done"].tap();
    
	// Change time
	window.scrollViews()[0].buttons()[2].tap();
	window.pickers()[0].wheels()[1].tapWithOptions({tapOffset:{x:0.77, y:0.33}});
	target.delay(1);
	app.toolbar().buttons()["Done"].tap();
    
	// change repeat
	window.scrollViews()[0].buttons()[3].tap();
	window.pickers()[0].wheels()[0].tapWithOptions({tapOffset:{x:0.33, y:0.32}});
	target.delay(1);
	app.toolbar().buttons()["Done"].tap();
    
	// Save
	app.navigationBar().rightButton().tap();
    
	if (window.tableViews()[0].cells().firstWithPredicate("name ENDSWITH [c] '"+ nameModified +"'").isValid())
	{
		UIALogger.logPass("Reminder Edited");
        
		return nameModified;
	}
	else
		UIALogger.logFail("Reminder Not Edited");
    
	return null;
}

function deleteOld(cellToDelete)
{
	var allCells = app.mainWindow().tableViews()[0].cells().length;
	var cell = app.mainWindow().tableViews()[0].cells().firstWithPredicate("name ENDSWITH [c] '"+ cellToDelete +"'");
    
	// Swipe
	cell.dragInsideWithOptions({startOffset:{x:0.09, y:0.24}, endOffset:{x:0.76, y:0.25}});
    
	// Delete button
	cell.buttons()[0].tap();
    
	if (cell.isValid())
		UIALogger.logFail("Reminder Not Deleted");
	else
		UIALogger.logPass("Reminder Deleted");
}

function quickAdd()
{
	// Add button
	app.navigationBar().rightButton().tap();
    
	// Set Title
	app.mainWindow().scrollViews()[0].textFields()[0].setValue("Quick Test");
    
	// Save
	app.navigationBar().rightButton().tap();
    
	if (cell.isValid())
		UIALogger.logPass("Reminder Added");
	else
		UIALogger.logFail("Reminder Not Added");
}

//quickAdd();

var newCell = addNew();
//addNew();
newCell  = editOld(newCell);
deleteOld(newCell);